package com.sunkyc.framework.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunkyc.commons.entity.PageQueryVo;
import com.sunkyc.commons.entity.TablePageData;
import com.sunkyc.framework.system.entity.SysLoginLog;
import com.sunkyc.framework.system.mapper.SysLoginLogMapper;
import com.sunkyc.framework.system.service.SysLoginLogService;
import com.sunkyc.framework.utils.TablePageDataBuilderUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 登录日志记录 服务实现类
 * </p>
 *
 * @author sunkyc
 * @since 2022-06-12
 */
@Service
@RequiredArgsConstructor
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {
    private final SysLoginLogMapper baseMapper;


    @Override
    public TablePageData<SysLoginLog> queryList(PageQueryVo pageQueryVo, SysLoginLog sysLoginLog) {
        Page<SysLoginLog> page = new Page<>(pageQueryVo.getCurrent(),pageQueryVo.getPageSize());
        IPage<SysLoginLog> iPage = baseMapper.selectPage(page,buildWrapper(sysLoginLog));
        return TablePageDataBuilderUtil.buildTablePageData(iPage);
    }

    @Override
    public int delLoginLog() {
        return baseMapper.delete(null);
    }


    private Wrapper<SysLoginLog> buildWrapper(SysLoginLog sysLoginLog){
        return Wrappers.lambdaQuery(SysLoginLog.class)
                .eq(StrUtil.isNotBlank(sysLoginLog.getLoginUsername()),SysLoginLog::getLoginUsername,sysLoginLog.getLoginUsername())
                .eq(ObjectUtil.isNotNull(sysLoginLog.getLoginState()),SysLoginLog::getLoginState,sysLoginLog.getLoginState())
                .orderByDesc(SysLoginLog::getLoginTime);
    }
}
